package com.dyzx.hbb.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.common.base.PageQuery;
import com.dyzx.hbb.common.base.PageResult;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.sys.entity.SysDict;
import com.dyzx.hbb.modules.sys.entity.SysDictValue;
import com.dyzx.hbb.modules.sys.query.DictValueQuery;
import com.dyzx.hbb.modules.sys.service.SysDictService;
import com.dyzx.hbb.modules.sys.service.SysDictValueService;
import com.dyzx.hbb.modules.sys.mapper.SysDictValueMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【sys_dict_value(字典值表)】的数据库操作Service实现
* @createDate 2025-04-21 16:07:41
*/
@Service
public class SysDictValueServiceImpl extends ServiceImpl<SysDictValueMapper, SysDictValue>
    implements SysDictValueService{
    @Autowired
    private SysDictService sysDictService;


    @Cacheable(cacheNames = "findByKeys", key = "#keys")
    @Override
    public Map<String, List<Map<String, Object>>> findByKeys(String keys) {
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        String[] split = keys.split(",");
        for (String s : split) {
            if (StringUtils.isNotEmpty(s)) {
                System.out.println(s);
                List<Map<String, Object>> byKey = this.findByKeyMap(s.trim());
                if (!byKey.isEmpty()) {

                    result.put(s.trim(), byKey);
                }

            }

        }
        return result;
    }

    @Cacheable(cacheNames = "findByKeys", key = "'all'")
    @Override
    public Map<String, List<Map<String, Object>>> findByKeyAll() {
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        List<SysDict> list = sysDictService.list();
        for (SysDict s : list) {

                System.out.println(s);
                List<Map<String, Object>> byKey = this.findByKeyMap(s.getKeyStr().trim());
                if (!byKey.isEmpty()) {

                    result.put(s.getKeyStr().trim(), byKey);
                }



        }
        return result;
    }

    @Cacheable(cacheNames = "findByMapKey", key = "#key")
    @Override
    public Map<Integer, SysDictValue> findByMapKey(String key) {
        Map<Integer, SysDictValue> map = new HashMap<>();
        MyLambdaQueryWrapper<SysDictValue> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysDictValue::getId)
                .select(SysDictValue::getName)

                .select(SysDictValue::getName).as("label")

                .select(SysDictValue::getValue)
                .select(SysDictValue::getParam)
                // .select(SysDictValue::getRemark)
                .select(SysDictValue::getColor);
        queryWrapper.inSql(SysDictValue::getDictId, "SELECT ID FROM sys_dict where key_str = '" + key + "'");
        List<SysDictValue> byKey = this.baseMapper.selectList(queryWrapper);
        for (SysDictValue sysDictValue : byKey) {
            System.out.println(sysDictValue);
            map.put(sysDictValue.getValue(), sysDictValue);
        }
        System.out.println(map);
        return map;
    }
    @Cacheable(cacheNames = "findByListKey", key = "#key")
    @Override
    public List<SysDictValue> findByListKey(String key) {
        String trim = StringUtils.trim(key);
        MyLambdaQueryWrapper<SysDictValue> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.inSql(SysDictValue::getDictId, "SELECT ID FROM sys_dict where key_str = '" + trim + "'");
        return this.baseMapper.selectList(queryWrapper);
    }

    private List<Map<String, Object>> findByKeyMap(String trim) {
        System.out.println(trim);
        MyLambdaQueryWrapper<SysDictValue> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper
                .select(SysDictValue::getName)
                .select(SysDictValue::getValue)

                .select(SysDictValue::getName).as("label")

                .select(SysDictValue::getParam)
               // .select(SysDictValue::getRemark)
                .select(SysDictValue::getColor);
                //.select(SysDictValue::getDictId)
               // .select(SysDictValue::getStatus);
        queryWrapper.inSql(SysDictValue::getDictId, "SELECT ID FROM sys_dict where key_str = '" + trim + "'");
        return this.baseMapper.selectMaps(queryWrapper);
    }
    @Override
    public PageResult<SysDictValue> findByPage(PageQuery pageQuery, DictValueQuery query) {
        MyLambdaQueryWrapper<SysDictValue> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysDictValue::getId)
                .select(SysDictValue::getName)
                .select(SysDictValue::getValue)
                .select(SysDictValue::getParam)
                .select(SysDictValue::getRemark)
                .select(SysDictValue::getColor)
                .select(SysDictValue::getDictId)
                .select(SysDictValue::getStatus)
                .select(SysDictValue::getCreateDate)
                .select(SysDictValue::getUpdateDate)
                .userName(SysDictValue::getOperatorId).as("operator_name")
                .select(SysDictValue::getOperatorId)
                .eq(SysDictValue::getDictId, query.getDict_id())
                .eq(query.getStatus()!=null,SysDictValue::getStatus, query.getStatus())
                .likeRight(StringUtils.isNotBlank(query.getName()),SysDictValue::getName, query.getName())
                .eq(query.getValue()!=null,SysDictValue::getValue, query.getValue())

                .likeRight(StringUtils.isNotBlank(query.getRemark()),SysDictValue::getRemark, query.getRemark())

                .orderByDesc(SysDictValue::getId);
        Page<SysDictValue> page = this.page(pageQuery.toPage(), wrapper);

        return PageResult.parsePage(page);

    }

    @Cacheable(cacheNames = "findByKey", key = "#key")
    @Override
    public List<Map<String, Object>> findByKey(String key) {
        MyLambdaQueryWrapper<SysDictValue> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper
                .select(SysDictValue::getName)

                .select(SysDictValue::getName).as("label")

                .select(SysDictValue::getValue)
                .select(SysDictValue::getParam)
                .select(SysDictValue::getColor);
        queryWrapper.inSql(SysDictValue::getDictId, "(SELECT ID FROM sys_dict where key_str = '" + key + "')");
        return this.baseMapper.selectMaps(queryWrapper);
    }

}




